package com.sh.data.engine.application.service.management;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.management.ApplicationKeyDTO;
import com.sh.data.engine.application.model.mapper.management.AkSkPageMapper;
import com.sh.data.engine.application.model.mapper.management.ApplicationKeyDTOMapper;
import com.sh.data.engine.application.model.request.management.SaveAkSkRequest;
import com.sh.data.engine.application.model.request.management.UpdateAkSkRequest;
import com.sh.data.engine.domain.authority.model.domain.ApplicationKeyDomain;
import com.sh.data.engine.domain.authority.service.AccessKeyService;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.management.model.param.BaseQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@Service
public class AccessKeyApplicationService {

    @Autowired
    private AccessKeyService accessKeyService;

    @Autowired
    private AkSkPageMapper akSKPageMapper;

    @Autowired
    private ApplicationKeyDTOMapper applicationKeyDTOMapper;

    public ApplicationKeyDTO createAccessKey(SaveAkSkRequest request) {
        return applicationKeyDTOMapper.domainToDTO(accessKeyService.createAccessKey(request.getDescription()));
    }

    public PagerResponse<ApplicationKeyDTO> getAccessKey(Integer pageNum, Integer pageSize) {
        var param = BaseQueryParam.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .build();
        PageResult<ApplicationKeyDomain> accessKey = accessKeyService.getAccessKey(param);
        return akSKPageMapper.toPageResponse(accessKey);
    }

    public int updateAKState(String accessKey) {
        return accessKeyService.updateAKState(accessKey);
    }

    public int updateDescription(UpdateAkSkRequest request) {
        return accessKeyService.updateDescription(request.getDescription(), request.getAccessKey());
    }

    public int deleteAccessKey(String accessKey) {
        return accessKeyService.deleteAccessKey(accessKey);
    }


}
